#!/bin/sh
#BK 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v407 fix for floppy drive icon.
#v409 gparted create ext3 part. failed, fixed by making /etc/mtab a symlink.
#120103 shinobar: need 'silent' option for ntfs-3g, avoid err msgs, permissions lost when copy.
#130203 change probedisk2 to probedisk.

#mount-FULL, umount-FULL, losetup-FULL are the full versions.
#The Busybox versions of mount and umount are available but only by:
# # busybox mount ...
# # busybox umount ...
#mount and umount are now scripts.
#if an ntfs partition, puppy uses user-mode ntfs-3g driver.
#the mount and umount scripts allow seamless mounting and unmounting of ntfs f.s.

[ ! $1 ] && exec busybox mount

#i realised this script has to allow reentrancy. So, all temp file now unique,
#using ${$} which is pid of script.
MYPID=${$}

. /etc/rc.d/functions4puppy4 #v4.02

#v2.12 discovered difference between $@ and $*. Replaced all $@ with $* in this script...

#extract all the '-' options, on separate lines... do NOT use $@!!!!...
#v3.93 eliminate ' -- ' and all past it...
DASHOPTS="`echo "$*" | tr '\t' ' '  | sed -e 's/ -- .*//' | tr ' ' '\n' | grep '^\-'`"

#needs an explicit '-t ntfs', does not work with /etc/fstab...
if [ "`echo "$*" | grep 'ntfs'`" = "" ];then

 # #busybox mount does not support '--bind'
 # #as have mtab file (see below), can now use full mount...
  #v409 put in '-n' option as now have /etc/mtab symlink to /proc/mounts...
  mount-FULL -n "${@}" #SFR quotes
  RETVAL=$?
 #fi
 
else
 #screen out all the options...
 CMDPRMS="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/' | tr '\n' ' '`"
 #kirk advised these options so Rox will not complain about file
 #permissions when copy a file to a ntfs partition...
 [ -f /tmp/ntfsmnterr${MYPID}.txt ] && rm -f /tmp/ntfsmnterr${MYPID}.txt
 
 ntfs-3g $CMDPRMS -o umask=0,no_def_opts,silent 2>/tmp/ntfsmnterr${MYPID}.txt #120103 shinobar: need silent
 RETVAL=$?
 #v2.16 ntfs-3g v1.417, part. scheduled for check, failed with value 10...
 #v4.00 ntfs-3g v1.2412 does not have 4,10, has 15 for dirty f.s., 14 hiberneted...
 if [ $RETVAL -eq 4 -o $RETVAL -eq 10 -o $RETVAL -eq 15 -o $RETVAL -eq 14 ];then  #try to force it...
  if [ $RETVAL -eq 14 ];then
   #ntfs-3g $CMDPRMS -o umask=0,no_def_opts,remove_hiberfile 2>/tmp/ntfsmnterr${MYPID}.txt
   #RETVAL=$?
   echo > /dev/null
  else
   ntfs-3g $CMDPRMS -o force,umask=0,no_def_opts,silent 2>/tmp/ntfsmnterr${MYPID}.txt #120103 shinobar: need silent.
   RETVAL=$?
   ERRMSG1="`cat /tmp/ntfsmnterr${MYPID}.txt`"
   echo "$ERRMSG1"
   if [ $RETVAL -eq 0 ];then
    echo "WARNING: NTFS f.s. mounted read/write but corrupted."
    [ ! "`pidof X`" = "" ] && nohup gxmessage -bg red -center -title "NTFS WARNING" "The ntfs-3g driver was able to mount the NTFS
partition but returned this error message:
$ERRMSG1

It is mounted read/write, but advice is only write
to it in emergency situation. Recommendation is
boot Windows and fix the filesystem first!!!" &
   fi
  fi
 fi
 
 #ntfs-3g plays very safe and will not mount if thinks anything
 #wrong with ntfs f.s. But, we may want to recover files from a
 #damaged windows. So, fall back to the kernel ntfs driver...
 if [ ! $RETVAL -eq 0 ];then
  #mount read-only...
  busybox mount -r -t ntfs $CMDPRMS
  RETVAL=$?
  ERRMSG1="`cat /tmp/ntfsmnterr${MYPID}.txt`"
  echo "$ERRMSG1"
  if [ $RETVAL -eq 0 ];then
   echo "WARNING: NTFS f.s. mounted read-only."
   [ ! "`pidof X`" = "" ] && nohup gxmessage -bg red -center -title "NTFS WARNING" "The ntfs-3g driver was unable to mount the NTFS
partition and returned this error message:
$ERRMSG1

So, the inbuilt kernel NTFS driver has been used
to mount the partition read-only." &
  fi
 fi
fi

#v4.02 if there is a desktop icon (see pup_eventd), then refresh it...
if [ $RETVAL -eq 0 -a "$DISPLAY" != "" ];then
 DEVNAME="`busybox mount | tail -n 1 | grep '^/dev/' | cut -f 1 -d ' ' | cut -f 3 -d '/'`"
 if [ "$DEVNAME" != "" ];then
  DRVNAME="`echo -n "$DEVNAME" | cut -c 1-3`"
  #special case, SD card /dev/mmcblk0p1...
  [ "$DRVNAME" = "mmc" ] && DRVNAME="`echo -n "$DEVNAME" | sed -e 's/p[0-9]$//'`"
  xDRVNAME="$DRVNAME" #v404
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition.
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
   case $DRVNAME in #v407
    fd*)
     DRV_CATEGORY="floppy"
    ;;
    *)
     dnPATTERN='/dev/'"${xDRVNAME}"'|'
     DRV_CATEGORY="`probedisk | grep "$dnPATTERN" | cut -f 2 -d '|'`"
    ;;
   esac
   icon_mounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
  fi
 fi
fi

#v409, instead just make sure the symlink stays there...
if [ ! -L /etc/mtab ];then
 rm -f /etc/mtab
 ln -s /proc/mounts /etc/mtab
fi

exit $RETVAL
